// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var tools = require("tools");
	var databind = require("databind");
	var css_insurance_detail = require("./loansMoneyCreate.css");
	var html_loansMoney = require("./html_loansMoney.html");
	var loansMoneyId;
	var init = function(page) {
		if (page.params != null) {
			loansMoneyId = page.params.loansMoneyId;
		}
		inquireBankInfoPagelist();
		inquireLendUnitInfoList();
		
		if (loansMoneyId != null) {
			$("#loansMoneyCreate .topButton").removeClass("none");
			$("#loansMoneyCreate .record").parent().removeClass("none");
			databind.fillInput1($("#loansMoneyCreate"), {"action": "inquireLonsMoneyInfoDetail.json?loansMoneyId="+loansMoneyId}, function(data){
				databind.disableInput($("#loansMoneyCreate"));
				$("#loansMoneyCreate .cancel").parent().html('<a href="javascript:history.go(-1);" class="cancel">返回</a>');
				var fs = $("#loansMoneyCreate input[name=fileUuids]").val().split(",");
				$("#loansMoneyCreate .loansMoney").remove();
				for (var j = 0; j < fs.length; j++) {
					if ($.trim(fs[j]) != "") {
						$("#loansMoneyCreate .fs").append('<div class="showProductIntroDiv"><a target="_blank" class="mas" fileUuid="' + fs[j] + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + fs[j] + '" class="showProductIntro">'+ tools.inquireFile(fs[j]) +'</a></div>');
					}
				}
				if (!data.datas[0].confirmState) {
					$("#loansMoneyCreate .return").remove();
					$("#loansMoneyCreate .addition").remove();
				}
				
				$("input[name=borrowedDateCount]").val($("input[name=borrowedDateCount]").val().substr(0, 10));
				$("input[name=borrowedDate]").val($("input[name=borrowedDate]").val().substr(0, 10));
				
				
			});
			
			tools.loadList("inquireLonsMoneyRecordPageList.json", $("#loansMoneyCreate .record"), $("#loansMoneyCreate #recordTpl"), {loansMoneyId: loansMoneyId}, function(){
				
			});
		}
		initEvent();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			location.href = "../login/login.html";
		}
		
		//提交
		$("#loansMoneyCreate .confirm").unbind("click").click(function(){
			databind.submitDom($("#loansMoneyCreate"), function(){
				base.alert(1, "拆入成功！");
				router.route("loansMoneyManage/loansMoneyList.html");
			});
		});
		
		//资金归还
		$("#loansMoneyCreate .return").unbind("click").click(function(){
			$("#popdialog").html(html_loansMoney);
			$("#loansMoney_dialog select[name=additionReason]").prev().remove().end().next().remove().end().next().remove().end().remove();
			$("#loansMoney_dialog .shouldReturn").removeClass("none");
			
			$("#loansMoney_dialog").attr("action", "returnLonsMoneyInfo");
			$("#loansMoney_dialog input[name=loansMoneyId]").val(loansMoneyId);
			$("#loansMoney_dialog").find(".dialogTitle").html("资金归还");
			
			$("#loansMoney_dialog .data").css("color", "red");
			$("#loansMoney_dialog .data").before("&#65509;");
			
			$("#loansMoney_dialog input[name=date]").on("blur", function() {
				var $this = $(this);
				if ($.trim($this.val()) != "") {
					databind.fillInput($("#loansMoney_dialog .shouldReturn"), {action: "calLonsMoney.json?loansMoneyId=" + loansMoneyId + "&cutoffDate=" + $this.val()});
				}
			});
			
			dialog.showDialog($("#loansMoney_dialog"));
			
			$("#loansMoney_dialog .dialogBtn").unbind("click").click(function(){
				databind.submitDom($("#loansMoney_dialog"), function(){
					base.alert(1, "归还成功");
					dialog.hideDialog($("#loansMoney_dialog"));
					router.route("loansMoneyManage/loansMoneyList.html");
				});
			});
		});
		
		//资金追加
		$("#loansMoneyCreate .addition").unbind("click").click(function(){
			$("#popdialog").html(html_loansMoney);
			tools.inquireCommonSelectorByStringList(11, $("#loansMoney_dialog select[name=additionReason]"));
			$("#loansMoney_dialog").attr("action", "additionLonsMoneyInfo");
			$("#loansMoney_dialog input[name=loansMoneyId]").val(loansMoneyId);
			$("#loansMoney_dialog").find(".dialogTitle").html("资金追加");
			dialog.showDialog($("#loansMoney_dialog"));
			
			$("#loansMoney_dialog .dialogBtn").unbind("click").click(function(){
				databind.submitDom($("#loansMoney_dialog"), function(){
					base.alert(1, "追加成功");
					dialog.hideDialog($("#loansMoney_dialog"));
					router.route("loansMoneyManage/loansMoneyList.html");
				});
			});
		});
		
		$("#loansMoneyCreate").on("change", "#loansMoney", function(){
			tools.uploadFile("loansMoney", function(data){
				var str = '<div class="showProductIntroDiv"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">'+ tools.inquireFile(data.data[0].fileUuid) +'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteProductIntro">x</a></div>';
				$("#loansMoneyCreate .fs").append(str);
				var fileUuid = [];
				for (var j = 0; j < $("#loansMoneyCreate .showProductIntroDiv .mas").length; j++) {
					fileUuid.push($($("#loansMoneyCreate .showProductIntroDiv .mas")[j]).attr("fileUuid"));
				}
				$("input[name=fileUuids]").val(fileUuid);
			});
		});
		
		$("#loansMoneyCreate").on("click", ".deleteProductIntro", function(){
			$(this).parent().remove();
			var fileUuid = [];
			for (var j = 0; j < $("#loansMoneyCreate .showProductIntroDiv .mas").length; j++) {
				fileUuid.push($($("#loansMoneyCreate .showProductIntroDiv .mas")[j]).attr("fileUuid"));
			}
			$("input[name=fileUuids]").val(fileUuid);
		});
	};
	
	var inquireBankInfoPagelist = function() {
		base.fnAjax({
			action: "inquireBlankInfoPagelist.json", //接口名称
			async: false,
			oData: {
				pageSize: 1000
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					$.each(data.datas, function() {
						var str = "<option value='"+ this.accountInfoId +"'>" + this.accountBank + "(" + this.accountBankName + ")" + "</option>";
						$("#loansMoneyCreate select[name=accountInfoId]").append(str);
					});
				}
			}
		});
	};
	
	var inquireLendUnitInfoList = function() {
		base.fnAjax({
			action: "inquireLendUnitInfoList.json", //接口名称
			async: false,
			oData: {
					}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					$.each(data.datas, function() {
						var str = "<option value='"+ this.lendUnitId +"'>" + this.lendUnitName + "</option>";
						$("#loansMoneyCreate select[name=lendUnitId]").append(str);
					});
				}
			}
		});
	};

	return {
		init: init
	};
});